﻿using System;
using System.IO;

namespace SilverlightApplication1.Models
{
    public class BlockModelBuilder
    {
        private const String DescritionDirectory = "RLS19G6/";

        private readonly BlockModel blockModel;
        public BlockModelBuilder(BlockModel blockModel)
        {
            this.blockModel = blockModel;
        }

        public BlockModelBuilder Called(String name)
        {
            blockModel.Name = name;
            return this;
        }

        public BlockModelBuilder WithText(String text)
        {
            blockModel.Text = text;
            return this;
        }

        public BlockModelBuilder WithDescriptionFromFile(String fileName)
        {
            if (fileName != null)
            {
                String path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), DescritionDirectory + fileName);
                if (File.Exists(path))
                {
                    using (TextReader blockTextReader = File.OpenText(path))
                    {
                        blockModel.Description = blockTextReader.ReadToEnd();
                    }
                }
            }
            return this;
        }

        public GalleryModelBuilder WithGallery
        {
            get
            {
                return new GalleryModelBuilder(new GalleryModel(), this);
            }
        }

        public static implicit operator BlockModel(BlockModelBuilder blockModelBuilder)
        {
            return blockModelBuilder.blockModel;
        }
    }
}
